<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="物料类型" prop="type">
				<el-select v-model="queryParams.type" placeholder="请选择物料类型" clearable size="small">
					<el-option label="名片" value="0"/>
					<el-option label="桌卡" value="1"/>
				</el-select>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
					<el-option label="申请中" value="0"/>
					<el-option label="已发货" value="1"/>
					<el-option label="已收货" value="2"/>
				</el-select>
			</el-form-item>
			<el-form-item label="医生姓名" prop="doctorName">
				<el-input
					v-model="queryParams.doctorName"
					placeholder="请输入医生姓名"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
<!--			<el-col :span="1.5">
				<el-button
					type="primary"
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
				>新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					icon="el-icon-edit"
					size="mini"
					:disabled="single"
					@click="handleUpdate"
				>修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
				>删除
				</el-button>
			</el-col>-->
			<el-col :span="1.5">
				<el-popconfirm
					confirm-button-text='当前页'
					cancel-button-text='全部'
					icon="el-icon-info"
					title="请选择导出内容"
					cancel-button-type="default"
					@confirm="handleExport(false)"
					@cancel="handleExport(true)"
				>
					<el-button
						type="warning"
						icon="el-icon-download"
						size="mini"
						:loading="exporting"
						slot="reference"
					>导出</el-button>
				</el-popconfirm>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
			<el-table-column type="index" :index="indexMethod" label="序号" width="55" align="center"/>
			<el-table-column label="物料类型" align="center" prop="type">
				<template slot-scope="scope">{{scope.row.type == 0 ? '名片': '桌卡'}}</template>
			</el-table-column>
			<el-table-column label="状态" align="center" prop="status">
				<template slot-scope="scope">{{scope.row.status|statusFilter}}</template>
			</el-table-column>
			<el-table-column label="医生姓名" align="center" prop="doctorName"/>
			<el-table-column label="收货人姓名" align="center" prop="userName"/>
			<el-table-column label="邮编" align="center" prop="postalCode"/>
			<el-table-column label="省" align="center" prop="provinceName"/>
			<el-table-column label="市" align="center" prop="cityName"/>
			<el-table-column label="区" align="center" prop="countyName"/>
			<el-table-column label="详细收货地址信息" align="center" prop="detailInfo"/>
			<el-table-column label="收货地址国家码" align="center" prop="nationalCode"/>
			<el-table-column label="收货人手机号码" align="center" prop="telNumber" width="150"/>
      <el-table-column label="申请时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
			<el-table-column label="操作" align="center" width="150">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-truck"
						:disabled="scope.row.status!=0"
						@click="deliverGoods(scope.row)"
					>发货
					</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-s-release"
						:disabled="scope.row.status!=1"
						@click="cancelDeliver(scope.row)"
					>取消
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改物料对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="物料类型" prop="type">
					<el-select v-model="form.type" placeholder="请选择物料类型">
						<el-option label="名片" value="0"/>
						<el-option label="桌卡" value="1"/>
					</el-select>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="form.status">
						<el-radio label="0">已申请</el-radio>
						<el-radio label="1">已发货</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="医生姓名" prop="doctorName">
					<el-input v-model="form.doctorName" placeholder="请输入医生姓名"/>
				</el-form-item>
				<el-form-item label="收货人姓名" prop="userName">
					<el-input v-model="form.userName" placeholder="请输入收货人姓名"/>
				</el-form-item>
				<el-form-item label="邮编" prop="postalCode">
					<el-input v-model="form.postalCode" placeholder="请输入邮编"/>
				</el-form-item>
				<el-form-item label="省" prop="provinceName">
					<el-input v-model="form.provinceName" placeholder="请输入省"/>
				</el-form-item>
				<el-form-item label="市" prop="cityName">
					<el-input v-model="form.cityName" placeholder="请输入市"/>
				</el-form-item>
				<el-form-item label="区" prop="countyName">
					<el-input v-model="form.countyName" placeholder="请输入区"/>
				</el-form-item>
				<el-form-item label="详细收货地址信息" prop="detailInfo">
					<el-input v-model="form.detailInfo" placeholder="请输入详细收货地址信息"/>
				</el-form-item>
				<el-form-item label="收货地址国家码" prop="nationalCode">
					<el-input v-model="form.nationalCode" placeholder="请输入收货地址国家码"/>
				</el-form-item>
				<el-form-item label="收货人手机号码" prop="telNumber">
					<el-input v-model="form.telNumber" placeholder="请输入收货人手机号码"/>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {
	listMaterial,
	getMaterial,
	delMaterial,
	addMaterial,
	updateMaterial,
	exportMaterial
} from "@/api/material/material";

export default {
	name: "Material",
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 物料表格数据
			materialList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				type: null,
				status: null,
				doctorName: null,
				userName: null,
				postalCode: null,
				provinceName: null,
				cityName: null,
				countyName: null,
				detailInfo: null,
				nationalCode: null,
				telNumber: null,
				orderByColumn: 'create_time',
				isAsc: 'desc'
			},
			exporting: false,
			// 表单参数
			form: {},
			// 表单校验
			rules: {}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询物料列表 */
		getList() {
			this.loading = true;
			listMaterial(this.queryParams).then(response => {
				this.materialList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				materialId: null,
				type: null,
				status: 0,
				doctorName: null,
				userName: null,
				postalCode: null,
				provinceName: null,
				cityName: null,
				countyName: null,
				detailInfo: null,
				nationalCode: null,
				telNumber: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.materialId)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加物料";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const materialId = row.materialId || this.ids
			getMaterial(materialId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改物料";
			});
		},
		deliverGoods(item) {
			this.$confirm('此操作将修改该申请状态至发货, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				item.status = 1;
				updateMaterial(item).then(response => {
					if (response.code === 200) {
						this.msgSuccess("修改成功");
						this.getList();
					}
				});
			})
		},
		cancelDeliver(item) {
			this.$confirm('您确定要取消发货吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				item.status = 0;
				updateMaterial(item).then(response => {
					if (response.code === 200) {
						this.msgSuccess("修改成功");
						this.getList();
					}
				});
			})
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.materialId != null) {
						updateMaterial(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							}
						});
					} else {
						addMaterial(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							}
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const materialIds = row.materialId || this.ids;
			this.$confirm('是否确认删除物料编号为"' + materialIds + '"的数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return delMaterial(materialIds);
			}).then(() => {
				this.getList();
				this.msgSuccess("删除成功");
			}).catch(function () {
			});
		},
		/** 导出按钮操作 */
		handleExport(flag) {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有物料数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(() => {
				this.exporting = true;
				if(flag) {
					return exportMaterial();
				} else {
					return exportMaterial(queryParams);
				}
			}).then(response => {
				this.download(response.msg);
				this.exporting = false;
			}).catch(function () {
			});
		},
		indexMethod(index) {
			return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
		}
	},
	filters: {
		statusFilter: function(text) {
			if(text == 0) {
				return '申请中';
			} else if(text == 1) {
				return '已发货';
			} else if(text == 2) {
				return '已收货';
			}
			return text
		}
	}
};
</script>
